var Code = -1
// var Host = "http://123.207.164.181:8080/ke/"
var Host = "https://www.weixiaoguo1990.com/"
var userinfo = {}
var resdata
function login(latitude, longitude) {
  var that = this;
  wx.login({
    success: function (res) {
      var code = res.code;
      DoLoginPost("isNewUser/", { code: code})
       
    }
  })
}

//////////////华丽的函数分割线
function DoLoginPost(url, data)   //PSOT分割
{
  var that = this
  wx.request({
    url: Host + url,
    method: "POST",
    data: data,
    header: {
      'content-type':'application/x-www-form-urlencoded'
    },
    success: function (res) {
      console.log(res);
      wx.setStorageSync('use', res.data);
    }, fail: function (res) {
    }
  })
}


////////华丽的分割线
function DoPost(url, data, fuc)   //PSOT分割
{
  var that = this
  var JWT = wx.getStorageSync('JWT')
  if (JWT == "" && JWT == undefined) {
    wx.showToast({
      title: '数据提交失败,请重新登陆',
      icon: 'loading',
      duration: 2000
    })
    return null;
  }
  else {
    var GETJWT = wx.getStorageSync('JWT')
    console.log("发送POST请求 url.." + Host + url)
    wx.request({
      url: Host + url,
      method: "POST",
      data: data,

      header: {
        'content-type': 'application/json',
        JWT: GETJWT
      }
      ,
      success: function (res) {
        console.log("POST请求成功")
        resdata = res.data.data
        console.log("请求结果为" + data)
        setTimeout(fuc, 200)
        return data
      },
      fail: function (res) {
        console.log("POST请求失败")
      }
    })
  }
}
////////华丽的分割线
function parseJwt()  //解析JWT
{
  var JWT = wx.getStorageSync('JWT')
  console.log("解析JWT" + JWT)
  var indexone = -1 //第一个点的下标
  var indextow = -1 //第二个点的下标
  if (JWT != "" && JWT != undefined) {
    for (var i = 0; i < JWT.length; i++)  //寻找第一个点
    {
      if (JWT[i] == '.') {
        indexone = i;
        for (var i2 = i + 1; i2 < JWT.length; i2++)  //寻找第二个点
        {
          if (JWT[i2] == '.') { indextow = i2; break; }
        }
        break;
      }
    }
    var info = JWT.substring(i + 1, i2)

    console.log("解析成功")
    var result = DoPost("decode", { Base64: info }, function () { wx.setStorageSync("userinfo", resdata) })

  }
  else {
    console.log("解析JWT失败")
    return "fail..."
  }
}

//暴露接口
module.exports = {
  login: login,
  parseJwt: parseJwt
}